<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          总共：{{ total }}条
        </template>
        <template #right>
          <el-button round size="small" type="warning" @click="$router.push('/employees/import')">导入 Excel</el-button>
          <el-button v-allow="'export_excel'" round size="small" type="danger">导出 Excel</el-button>
          <el-button round size="small" type="primary" @click="showdialog=true">新增员工</el-button>
        </template>
      </page-tools>
      <el-card style="margin-top: 10px;">
        <el-table border :data="employees">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像">
            <template slot-scope="scope">
              <image-holder :src="scope.row.staffPhoto" />
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式">
            <template v-slot="{row}">
              {{ row.formOfEmployment === 1?'正式':'非正式' }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column sortable label="入职时间">
            <template v-slot="{row}">
              {{ formDate(row.timeOfEntry) }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="280">
            <template v-slot="{row}">
              <el-button type="text" size="small" @click="$router.push('/employees/detail/'+ row.id)">查看</el-button>
              <el-button type="text" size="small" @click="hFen(row.id)">分配角色</el-button>
              <el-button type="text" size="small" @click="hDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-pagination
          :current-page="q.page"
          :page-sizes="[5, 10, 15, 30]"
          :page-size="q.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-card>
    </div>
    <el-dialog
      title="新增员工"
      :visible.sync="showdialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="40%"
    >
      <addorEdit v-if="showdialog" @close="showdialog=false" @success="hsuccess" />
    </el-dialog>
    <el-dialog
      title="权限分配"
      :visible.sync="Mshowdialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="40%"
    >
      <assign-role :id="codId" ref="assignRole" @close="Mshowdialog=false" />
    </el-dialog>
  </div>
</template>
<script>
import { getfullEmployee, removeEmployee } from '@/api/employee'
import addorEdit from './empDialog'
import dayjs from 'dayjs'
import AssignRole from './assignRole.vue'
export default {
  components: {
    addorEdit,
    AssignRole
  },
  data() {
    return {
      employees: [],
      total: 0,
      q: {
        page: 1,
        size: 10
      },
      showdialog: false,
      Mshowdialog: false,
      codId: ''
    }
  },
  created() {
    this.loadEmployess()
  },
  methods: {
    async loadEmployess() {
      const { data: res } = await getfullEmployee(this.q)
      this.employees = res.data.rows
      this.total = res.data.total
    },
    handleSizeChange(size) {
      // console.log(size)
      this.q.size = size
      this.loadEmployess()
    },
    handleCurrentChange(page) {
      // console.log(page)
      this.q.page = page
      this.loadEmployess()
    },
    hDel(id) {
      this.$confirm('真的要删除吗？宝贝', '提示', {
        type: 'warning'
      }).then(async() => {
        try {
          const { data: res } = await removeEmployee(id)
          this.$message.success(res.message)
          if (this.employees.length === 1) {
            this.q.page--
            if (this.q.page <= 0) {
              this.q.page = 1
            }
          }
          this.loadEmployess()
        } catch (e) {
          this.$message.error(e.message)
        }
      }).catch(() => {

      })
    },
    hsuccess() {
      this.showdialog = false
      this.loadEmployess()
    },
    formDate(date) {
      return dayjs(date).format('YYYY-MM-DD')
    },
    hFen(id) {
      // console.log(id)
      this.Mshowdialog = true
      this.codId = id
      this.$nextTick(() => {
        this.$refs.assignRole.loadUserRoles()
      }
      )
    }
  }
}
</script>
<style scoped>
.avatar{
  height: 50px;
  width: 50px;
  border-radius: 50%;
}
.el-pagination{
display: flex;
height: 60px;
justify-content: center;
align-items: center;
margin-top: 20px;
}
</style>
